package com.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;


@Api(tags = "邮件发送通用接口")
@RestController
public class EmailController {

    @Autowired
    private JavaMailSender jms;

    @Value("${spring.mail.username}")
    private String username;

    @RequestMapping(value = "/SendEmail",method = RequestMethod.POST)
    public String SendEmail(){
        System.out.println("进入发送普通邮件的方法");
        String str = null;
        try{
            //声明邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            //通过邮箱对象设置相关参数
            message.setFrom(username);
            message.setTo("1578661255@qq.com");
            message.setSubject("浅发一封简单的邮件吧！");
            message.setText("欢迎假期来学习大数据应用技术!!!");
//            message.setCc("1578661255@qq.com");
            //发送邮件
            jms.send(message);
            str = "邮件发送成功OK";
        }catch(Exception e){
            System.out.println("简单邮件发送失败！原因为："+e.getMessage());
            str = "发送失败NO";
        }
        return str;
    }

    @RequestMapping(value = "/SendHtmlEmail",method = RequestMethod.POST)
    public String SendHtmlEmail(){
        System.out.println("进入发送html内容邮件的方法");
        String str = "";
        try{
            MimeMessage message = null;
            message = jms.createMimeMessage();

            MimeMessageHelper html = new MimeMessageHelper(message,true);
            html.setFrom(username);
            html.setTo("1578661255@qq.com");
            html.setSubject("html的一封邮件");

            //声明带有html命令格式的内容
            StringBuffer sb = new StringBuffer("<p style='color:#42b98e'>使用大数据分析内容"+
                    "通过html格式将内容发送到网页上。</p>");

             html.setText(sb.toString(),true);

             jms.send(message);

             str = "html邮件发送成功！";
        }catch (Exception ex){
            System.out.println("html邮件发送失败，原因为："+ex.getMessage());
            str = "html邮件发送失败。";
        }
        return str;
    }

    @RequestMapping(value = "SendFuJianEmail",method = RequestMethod.POST)
    public String SendFuJianEmail(){
        System.out.println("进入发送附件的邮件方法");
        String str = "";
        try{
            MimeMessage message = null;
            message = jms.createMimeMessage();
            MimeMessageHelper html = new MimeMessageHelper(message,true,"UTF-8");

            html.setFrom(username);
            html.setTo("1578661255@qq.com");
            html.setSubject("发送一个带附件的邮件");
            html.setText("详情请下载附件：");
            FileSystemResource file = new FileSystemResource("src\\main\\resources\\static.file\\测试结果.docx");
            html.addAttachment("测试结果.docx",file);

            jms.send(message);
            str="发送带附件的邮件成功！";

        }catch (Exception e){
            System.out.println("发送附件邮件失败，原因为："+e.getMessage());
            str = "附件发送失败！";
        }
        return str;
    }

    @RequestMapping(value = "/SendImgEmail", method = RequestMethod.POST)
    public String SengImgEmail(){
        System.out.println("进入发送带有图片的网页方法");
        String str = "";
        try{
            MimeMessage message = null;
            message = jms.createMimeMessage();
            MimeMessageHelper html = new MimeMessageHelper(message,true,"UTF-8");

            html.setFrom(username);
            html.setTo("1578661255@qq.com");
            html.setSubject("发送一个带图片的邮件");
            String txt = "应该能看到吧呜呜呜：<img src='cid:img'/>";
            html.setText(txt,true);
            FileSystemResource fileImg = new FileSystemResource("src\\main\\resources\\img\\haha.jpg");
//            html.addAttachment("haha.jpg",fileImg);
            html.addInline("img",fileImg);

            jms.send(message);
            str = "带有图片网页的邮件发送成功！";
        }catch (Exception e){
            System.out.println("发送带有图片的网页失败，原因为："+e.getMessage());
            str = "发送带有图片的网页失败！";
        }
        return str;
    }

}
